﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using NLite.Data.SqlTypes;
using System.Data.Common;
using NLite.Data.Schema;
using NLite.Data.Driver.Internal;
using NLite.Data.Schema.Provider.Internal;

namespace NLite.Data.Driver
{
    public class JetDriver:OledbDriver
    {
        public JetDriver()
        {
            SchemaProvider =new  AccessSchemaProvider();
        }
        protected override void InitializeParameter(System.Data.IDbDataParameter cp, IDataParameter p)
        {
            cp.ParameterName = p.Name;
            cp.Value = p.Value;
            cp.Direction = p.Identity.Direction;

            if (cp.Direction == ParameterDirection.Output 
                || cp.Direction == ParameterDirection.ReturnValue)
                return;

            switch (p.Identity.Type.DbType)
            {
                case DbTypeCode.Clob:
                    cp.DbType = System.Data.DbType.AnsiString;
                    break;
                case DbTypeCode.NClob:
                    cp.DbType = System.Data.DbType.String;
                    break;
                case DbTypeCode.Blob:
                    cp.DbType = System.Data.DbType.Binary;
                    break;
                case DbTypeCode.Date:
                case DbTypeCode.DateTime:
                case DbTypeCode.Time:
                    cp.DbType = DbType.String;
                    if (cp.Value != DBNull.Value && !object.Equals(null,cp.Value))
                        cp.Value = ((DateTime)cp.Value).ToString("yyyy/MM/dd HH:mm:ss");
                    break;
                case DbTypeCode.Int64:
                case DbTypeCode.UInt64:
                    cp.DbType = DbType.Int32;
                    if (cp.Value != DBNull.Value && !object.Equals(null, cp.Value))
                        cp.Value = Convert.ToInt32((long)cp.Value);
                    break;
                default:
                    cp.DbType = (DbType)p.Identity.Type.DbType;
                    break;
            }
        }

       
        
    }

}
